var db = require('../db')
var $sql = require('../map/thaiMap')

module.exports = {
	// 创建用户浏览日志
  createStoreUserLog: function (req) {
    db.query($sql.createStoreUserLog, [+req.storeId, +req.userId], function (err, results, fields) {
      if (err) throw new Error(err)
      console.log(results.insertId)
    })
  },
	// 获取浏览次数
  getStoreViewNum: function (req, callback) {
    db.query($sql.getStoreViewNum, [+req.storeId], function (err, results, fields) {
      if (err) throw new Error(err)

      console.log(results[0])

      callback(err, results[0])
    })
  },
	// 提价评论
  submitUserComment: function (req, callback) {
    db.query($sql.submitUserComment, [+req.storeId, req.userName, req.userAvatar, req.comment], function (err, results, fields) {
      if (err) throw new Error(err)

      console.log(results, results.insertId)

      callback(err, results.insertId)
    })
  },
	// 获取店铺评论列表
  getStoreCommentList: function (req, callback) {
    db.query($sql.getStoreCommentList, +req.storeId, function (err, results, fields) {
      if (err) throw new Error(err)

      console.log(results[0], results.length)

      callback(err, results)
    })
  },
  getDistrictByCity: function (req, callback) {
    db.query($sql.getDistrictByCity, +req.city_index, function (err, results, fields) {
      if (err) throw new Error(err)

      console.log(results[0], results.length)

      callback(err, results)
    })
  },
  getStoreGoodById: function (req, callback) {
    db.query($sql.getStoreGoodById, +req.store_id, function (err, results, fields) {
      try {
        console.log('getStoreGoodById: ', results)
        callback(null, results)
      } catch (err) {
        callback(err, results)
        console.error(err, new Date())
      }
    })
  },
  getStoreById: function (req, callback) {
    console.log('getStoreById:', req.store_id)
    db.query($sql.getStoreById, +req.store_id, function (err, results, fields) {
      try {
        console.log('getStoreById: ', results[0])
        callback(null, results[0])
      } catch (err) {
        callback(err, results[0])
        console.error(err, new Date())
      }
    })
  },
	// 根据地址选择店铺
  getStoreListByAddress: function (req, callback) {
    db.query($sql.getStoreListByAddress, +req.city_index, function (err, results, fields) {
      try {
        console.log('getStoreListByAddress: ', results)
        callback(null, results)
      } catch (err) {
        callback(err, results)
        console.error(err, new Date())
      }
    })
  },
	// 根据种类选择店铺
  getStoreListByType: function (req, callback) {
    db.query($sql.getStoreListByType, +req.store_type, function (err, results, fields) {
      try {
        console.log('getStoreListByType', results)
        callback(null, results)
      } catch (err) {
        callback(err, results)
        console.error(err, new Date())
      }
    })
  },
	// 获取店铺列表
  getStoreList: function (req, callback) {
    db.query($sql.getStoreList, null, function (err, results, fields) {
      try {
        console.log('getStoreList', results)
        callback(null, results)
      } catch (err) {
        callback(err, results)
        console.error(err, new Date())
      }
    })
  }
}
